package com.huateng.springcloud.datasource;


import com.google.common.collect.Maps;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;


/**
 * @author Evan
 * @create 2021/7/20 9:56
 */
@Endpoint(id = "datasource")
public class DataSourceEndpoint {

    private HTDataSource htDataSource;

    public DataSourceEndpoint(HTDataSource htDataSource) {
        this.htDataSource = htDataSource;
    }

    @ReadOperation
    public Map<String, Object> pool(){
        GenericObjectPool<HTConnection> pool = htDataSource.getPool();
        HashMap<String, Object> map = Maps.newHashMap();
        map.put("numActive", pool.getNumActive());
        map.put("numIdle", pool.getNumIdle());
        map.put("createdCount", pool.getCreatedCount());
        return map;
    }

}
